package cn.yudao.pet.controller;

import cn.yudao.basic.exception.BusinessException;
import cn.yudao.basic.utils.LoginContext;
import cn.yudao.org.domain.Department;
import cn.yudao.org.domain.Employee;
import cn.yudao.org.mapper.EmployeeMapper;
import cn.yudao.pet.domain.PetDetail;
import cn.yudao.pet.service.IPetService;
import cn.yudao.pet.domain.Pet;
import cn.yudao.pet.query.PetQuery;
import cn.yudao.basic.utils.AjaxResult;
import cn.yudao.basic.utils.PageList;
import cn.yudao.user.domain.Logininfo;
import cn.yudao.user.mapper.LogininfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    public IPetService petService;



    @Autowired
    public EmployeeMapper employeeMapper;

    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrupadte(@RequestBody Pet pet, HttpServletRequest request){
        try {
            if( pet.getId()!=null)
                petService.upadte(pet);
            else{
                Logininfo logininfo = LoginContext.getLogininfo(request);
                Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
                pet.setShopId(employee.getShopId());
                petService.save(pet);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Pet get(@PathVariable("id")Long id)
    {
        return petService.LoadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Pet> list(){

        return petService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Pet> json(@RequestBody PetQuery query)
    {
        return petService.query(query);
    }


    /**
    * @Title: getDetail
    * @Description: 获取宠物详情
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/16 20:39
    * @Parameters:
    * @Return
    */
    @GetMapping("/detail/{id}")
    public AjaxResult getDetail(@PathVariable("id") Long id){
        try {
               PetDetail petDetail =  petService.loadDetailByPetId(id);
            return AjaxResult.me().setResult(petDetail);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    /**
    * @Title: patchRom
    * @Description: 批量上架
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/16 21:12
    * @Parameters:
    * @Return
    */
    @PostMapping("/onsale")
    public AjaxResult onSalePet(@RequestBody Long[] ids){
        try {
            petService.onSalePet(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }


    /**
    * @Title: onSalePet
    * @Description: 批量下架
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/16 21:14
    * @Parameters:
    * @Return
    */
    @PostMapping("/offsale")
    public AjaxResult offSalePet(@RequestBody Long[] ids){
        try {
            petService.offSalePet(ids);
            return AjaxResult.me();
        } catch (BusinessException e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }


}
